package com.heshi.producthomework.mapper;

import com.heshi.producthomework.entity.Product;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ProductMapper {
    @Insert("insert into product(product_id,title,price,in_time,count) values(#{productId},#{title},#{price},#{inTime},#{count})")
    Integer insertProduct(Product product);

    @Select("select id, product_id , title, price, in_time , count from product where title like concat('%', #{keyword}, '%') order by id desc")
    java.util.List<Product> selectByTitleLike(@Param("keyword") String keyword);

    @Select("select null as id, product_id as productId, max(title) as title, null as price, null as inTime, sum(count) as count from product group by product_id having sum(count) < 10 order by product_id asc")
    java.util.List<Product> selectLowStockAggregated();

    @Select("select id, product_id as productId, title, price, in_time as inTime, count from product order by id desc limit #{offset}, #{pageSize}")
    java.util.List<Product> selectPage(@Param("offset") int offset, @Param("pageSize") int pageSize);
}
